package excercise.other;

/**
 * 判断一个Integer是否为回文数字
 */
public class IsPalindromicNumber {

    public boolean isPalindromic(int n) {

        int offset = 1;
        int tempN = n;

        // 除法可以防止数值溢出的问题
        while (tempN / 10 > 0) {
            offset *= 10;
            tempN /= 10;
        }

        while (offset != 0 && n / offset == (n % 10)) {
            n = (n - (n / offset) * offset) / 10;
            offset /= 100;
        }

        return n < 10;
    }

    public static void main(String[] args) {
        IsPalindromicNumber isPalindromicNumber = new IsPalindromicNumber();
        System.out.println("isPalindromicNumber.isPalindromic(13231) = " + isPalindromicNumber.isPalindromic(13231));
        System.out.println("isPalindromicNumber.isPalindromic(13231) = " + isPalindromicNumber.isPalindromic(1323));
        System.out.println("isPalindromicNumber.isPalindromic(13231) = " + isPalindromicNumber.isPalindromic(134565431));
        System.out.println("isPalindromicNumber.isPalindromic(13231) = " + isPalindromicNumber.isPalindromic(13455431));
    }
}
